package springinpractice.ch2.controller;
import java.math.BigDecimal;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import springinpractice.ch2.model.Department;
import springinpractice.ch2.model.Email;
import springinpractice.ch2.model.Employee;

/*
 * @author	Willie Wheeler John Wheeler and Zeeshan Maqbool
 * Book		'Spring In Practice' [Book written by Willie Wheeler and John Wheeler]
 *
 * This class contains code from different pages of the book which hasnt been utilized completely
 * 
 * Also, you can try to embed Email inside Employee class using @Embeddable
 */

public class Temp
{
	private SessionFactory sessionFactory;
	Session session = sessionFactory.getCurrentSession();
	
	private void Page44()
	{
		Employee employee = new Employee();
		
		employee.setEmp_name("John Wheeler");
		session.save(employee);
		Employee employee2 = (Employee) session.get(Employee.class, 1);
		employee2.setTitle("Application Developer");
		
		session.delete(employee2);
	}
	
	private void Page54()
	{
		Employee john = new Employee("John Wheeler", "Software Engineer", new BigDecimal("25000"), new Date());
		
		//john.getEmails().add("jwheeler1@gmail.com");
		//john.getEmails().add("jwheeler@riversideca.gov");
		
		session.save(john);
	}
	
	private void Page56()
	{
		Employee employee = new Employee("John Wheeler", "Software Engineer",new BigDecimal("25000"), new Date());
		
		//employee.getEmails().add(new Email("jwheeler1@gmail.com", "Home"));
		//employee.getEmails().add(new Email("jwheeler@riversideca.gov", "Work"));
		
		session.save(employee);
	}
	
	private void Page57()
	{
		Department infotech = new Department("Information Technology");
		Employee john = new Employee("John Wheeler", "Software Engineer",new BigDecimal("25000"), new Date());
		Employee willie = new Employee("Willie Wheeler", "Software Engineer",new BigDecimal("25000"), new Date());
		john.setDepartment(infotech);
		willie.setDepartment(infotech);
		
		session.save(infotech);
		session.save(john);
		session.save(willie);
	}
	
	private void Page59()
	{
		Department department = new Department("Information Technology");
		//department.addEmployee(new Employee("John Wheeler", "Software Engineer", new BigDecimal("25000"), new Date()));
		//department.addEmployee(new Employee("Willie Wheeler", "Software Engineer", new BigDecimal("25000"), new Date()));
		
		session.save(department);
	}
	
	private void Page60()
	{
		session.createQuery("from Employee, Department").list();
		
	}
	
	private void Page62()
	{
		String name = "temp";
		String namePrefix = "temp";
		session.createQuery("from Employee e where e.name = :name").setParameter("name", name).list();

		session.createQuery("from Employee as e where e.name like :namePrefix")
			.setParameter("namePrefix", namePrefix + "%")
			.list();
		
		session.createQuery("from Employee as e " +
			"where e.salary between :minSalary and :maxSalary")
			.setParameter("minSalary", new BigDecimal("10000"))
			.setParameter("maxSalary", new BigDecimal("25000"))
			.list();
		
		session.createQuery("from Employee e "
				+ "where e.department.name = 'Information Technology'")
				.list();
	}

	private void Page63()
	{
		session.createQuery("select e from Employee e inner join e.department d "
				+ "where d.name = 'Information Technology'")
				.list();
	}
	
	private void Page64()
	{
		Long count = (Long) session.createQuery("select count(e) from Employee e").uniqueResult();
	}
}
